197. Rising Temperature


Posted by ikl258794613 on 2024-02-22

Column Name Type
id int
recordDate date
temperature int

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

Input:
Weather table:

id recordDate temperature
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

Output:

id
2
4

Explanation:
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).

解答:

SELECT w1.id FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.recordDate,w2.recordDate) = 1 
AND w1.temperature > w2.temperature

學習點:
1.自己跟自己比較時SELECT自己兩次。
2.DATEDIFF ( datepart , startdate , enddate ) datepart不傳時返回兩個日期之間的差異。


#SQL







Related Posts

DEC 14 2023 PROCL-5: User does not have permission to perform a local registry operation on this key. Authentication error [User [root] [已解決]

DEC 14 2023 PROCL-5: User does not have permission to perform a local registry operation on this key. Authentication error [User [root] [已解決]

Day03:從迴圈看 bytecode

Day03:從迴圈看 bytecode

[TensorFlow Certification Day4] TensorFlow的人工智能, 機器學習和深度學習簡介 Week4

[TensorFlow Certification Day4] TensorFlow的人工智能, 機器學習和深度學習簡介 Week4


Comments